package com.example.pingze.service;

import com.example.pingze.entity.RecommendPicture;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.RecommendPictureRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Service
public class RecommendPictureService {
    @Autowired
    private RecommendPictureRepository repository;

    public RecommendPicture findRecommendPictureByPictureTime(String pictureTime) {
        RecommendPicture picture = repository.findRecommendPictureByPictureTime(pictureTime);
        return picture;
    }

    public Page getRecommendPictureList(RecommendPicture recommendPicture, Pageable pageable) {
        Specification<RecommendPicture> specification = new Specification<RecommendPicture>() {
            @Override
            public Predicate toPredicate(Root<RecommendPicture> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if(!StringUtils.isEmpty(recommendPicture.getId())){
                    predicates.add(criteriaBuilder.equal(root.get("id"),recommendPicture.getId()));
                }
                if(!StringUtils.isEmpty(recommendPicture.getPictureTime())){
                    predicates.add(criteriaBuilder.equal(root.get("pictureTime"),recommendPicture.getPictureTime()));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        Page page = repository.findAll(specification,pageable);
        return page;
    }

    public RecommendPicture add(String pictureUrl, String pictureTime) {
        if(StringUtils.isEmpty(pictureUrl)){
            throw new PingZeException("图片路径为空");
        }
        if(StringUtils.isEmpty(pictureTime)){
            throw new PingZeException("图片创建时间不能为空");
        }
        RecommendPicture add = new RecommendPicture();
        add.setId(UUID.randomUUID().toString());
        add.setPictureUrl(pictureUrl);
        add.setPictureTime(pictureTime);
        repository.save(add);
        return add;
    }

    public RecommendPicture edit(String id, String pictureUrl, String pictureTime) {
        RecommendPicture edit = repository.findRecommendPictureById(id);
        if(!edit.getPictureUrl().equals(pictureUrl)){
            edit.setPictureUrl(pictureUrl);
        }
        if(!edit.getPictureTime().equals(pictureTime)){
            edit.setPictureTime(pictureTime);
        }
        repository.save(edit);
        return edit;
    }

    public RecommendPicture findRecommendPictureById(String id) {
        return repository.findRecommendPictureById(id);
    }

    public void delete(RecommendPicture find) {
        repository.delete(find);
    }
}
